home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kuser.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  10.3 KB  |  385 lines

  1. /*
  2.  *  KUser - represent a user/account
  3.  *  Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
  4.  *  Copyright (C) 2003 Oswald Buddenhagen <ossi@kde.org>
  5.  *  Copyright (C) 2004 Jan Schaefer <j_schaef@informatik.uni-kl.de>
  6.  *
  7.  *  This library is free software; you can redistribute it and/or
  8.  *  modify it under the terms of the GNU Library General Public
  9.  *  License as published by the Free Software Foundation; either
  10.  *  version 2 of the License, or (at your option) any later version.
  11.  *
  12.  *  This library is distributed in the hope that it will be useful,
  13.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  *  Library General Public License for more details.
  16.  *
  17.  *  You should have received a copy of the GNU Library General Public License
  18.  *  along with this library; see the file COPYING.LIB.  If not, write to
  19.  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  20.  *  Boston, MA 02110-1301, USA.
  21.  */
  22. #ifndef KUSER_H
  23. #define KUSER_H
  24.  
  25. #include "ksharedptr.h"
  26.  
  27. class KUserGroup;
  28. class QString;
  29. class QStringList;
  30. class KUserPrivate;
  31. struct passwd;
  32. template <class T> class QValueList;
  33.  
  34. /**
  35.  * @short Represents a user on your system
  36.  *
  37.  * This class represents a user on your system. You can either get
  38.  * information about the current user, of fetch information about
  39.  * a user on the system. Instances of this class will be explicitly shared,
  40.  * so copying objects is very cheap and you can safely pass objects by value.
  41.  *
  42.  * @author Tim Jansen <tim@tjansen.de>
  43.  * @since 3.2
  44.  */
  45. class KDECORE_EXPORT KUser {
  46.  
  47. public:
  48.  
  49.   enum UIDMode { 
  50.       UseEffectiveUID, ///< Use the effective user id.
  51.       UseRealUserID    ///< Use the real user id.
  52.   };
  53.  
  54.   /**
  55.    * Creates an object that contains information about the current user.
  56.    * (as returned by getuid(2) or geteuid(2), taking $LOGNAME/$USER into
  57.    * account).
  58.    * @param mode if #UseEffectiveUID is passed the effective
  59.    *             user is returned. 
  60.    *        If #UseRealUserID is passed the real user will be
  61.    *        returned. 
  62.    *        The real UID will be different than the effective UID in setuid
  63.    *        programs; in  
  64.    *        such a case use the effective UID for checking permissions, and
  65.    *        the real UID for displaying information about the user.
  66.    */
  67.   // XXX KDE4: Make this explicit
  68.   KUser(UIDMode mode = UseEffectiveUID);
  69.  
  70.   /**
  71.    * Creates an object for the user with the given user id.
  72.    * If the user does not exist isValid() will return false.
  73.    * @param uid the user id
  74.    */
  75.   // XXX KDE4: Make this explicit; give parameter as uid_t instead of "long"
  76.   KUser(long uid);
  77.  
  78.   /**
  79.    * Creates an object that contains information about the user with the given
  80.    * name. If the user does not exist isValid() will return false.
  81.    *
  82.    * @param name the name of the user
  83.    */
  84.   // XXX KDE4: Make this explicit
  85.   KUser(const QString& name);
  86.  
  87.   /**
  88.    * Creates an object that contains information about the user with the given
  89.    * name. If the user does not exist isValid() will return false.
  90.    *
  91.    * @param name the name of the user
  92.    */
  93.   // XXX KDE4: Make this explicit
  94.   KUser(const char *name);
  95.  
  96.   /**
  97.    * Creates an object from a passwd structure.
  98.    * If the pointer is null isValid() will return false.
  99.    *
  100.    * @param p the passwd structure to create the user from
  101.    */
  102.   // XXX KDE4: Make this explicit
  103.   KUser(struct passwd *p);
  104.  
  105.   /**
  106.    * Creates an object from another KUser object
  107.    * @param user the user to create the new object from
  108.    */
  109.   KUser(const KUser & user);
  110.   
  111.   /**
  112.    * Copies a user
  113.    * @param user the user to copy
  114.    * @return this object
  115.    */
  116.   KUser& operator =(const KUser& user);
  117.   
  118.   /**
  119.    * Two KUser objects are equal if isValid() is true 
  120.    * and the uid() are identical.
  121.    */
  122.   bool operator ==(const KUser& user) const;
  123.  
  124.   /**
  125.    * Two KUser objects are not equal if either isValid() is
  126.    * not true or uid() are not identical.
  127.    */
  128.   bool operator !=(const KUser &user) const;
  129.  
  130.   /**
  131.    * Returns true if the user is valid. A KUser object can be invalid if 
  132.    * you created it with an non-existing uid or name.
  133.    * @return true if the user is valid
  134.    */
  135.   bool isValid() const;
  136.  
  137.   /**
  138.    * Returns the user id of the user.
  139.    * @return the id of the user or -1 if user is invalid
  140.    */
  141.   // XXX KDE4: Make this return uid_t
  142.   long uid() const;
  143.  
  144.  
  145.   /**
  146.    * Returns the group id of the user.
  147.    * @return the id of the group or -1 if user is invalid
  148.    */
  149.   // XXX KDE4: Make this return gid_t
  150.   long gid() const;
  151.  
  152.   /**
  153.    * Checks whether the user it the super user (root).
  154.    * @return true if the user is root
  155.    */
  156.   bool isSuperUser() const;
  157.  
  158.   /**
  159.    * The login name of the user.
  160.    * @return the login name of the user or QString::null if user is invalid 
  161.    */
  162.   QString loginName() const;
  163.  
  164.   /**
  165.    * The full name of the user.
  166.    * @return the full name of the user or QString::null if user is invalid
  167.    */
  168.   QString fullName() const;
  169.  
  170.   /**
  171.    * The user's room number.
  172.    * @return the room number of the user or QString::null if not set or the
  173.    *         user is invalid 
  174.    */
  175.   QString roomNumber() const;
  176.  
  177.   /**
  178.    * The user's work phone.
  179.    * @return the work phone of the user or QString::null if not set or the
  180.    *         user is invalid 
  181.    */
  182.   QString workPhone() const;
  183.  
  184.   /**
  185.    * The user's home phone.
  186.    * @return the home phone of the user or QString::null if not set or the
  187.    *         user is invalid 
  188.    */
  189.   QString homePhone() const;
  190.  
  191.   /**
  192.    * The path to the user's home directory.
  193.    * @return the home directory of the user or QString::null if the
  194.    *         user is invalid 
  195.    */
  196.   QString homeDir() const;
  197.  
  198.   /**
  199.    * The path to the user's login shell.
  200.    * @return the login shell of the user or QString::null if the
  201.    *         user is invalid 
  202.    */
  203.   QString shell() const;
  204.  
  205.   /**
  206.    * Returns all groups of the user
  207.    * @return all groups of the user
  208.    */
  209.   QValueList<KUserGroup> groups() const;
  210.   
  211.   /**
  212.    * Returns all group names of the user
  213.    * @return all group names of the user
  214.    */
  215.   QStringList groupNames() const;
  216.   
  217.   
  218.   /**
  219.    * Destructor.
  220.    */
  221.   ~KUser();
  222.  
  223.   /** 
  224.    * Returns all users of the system.
  225.    * @return all users of the system.
  226.    */
  227.   static QValueList<KUser> allUsers();
  228.  
  229.   /** 
  230.    * Returns all user names of the system.
  231.    * @return all user names of the system.
  232.    */
  233.   static QStringList allUserNames();
  234.  
  235. private:
  236.   KSharedPtr<KUserPrivate> d;
  237.   void fillPasswd(struct passwd* p);
  238.   void fillName(const char* name);
  239. };
  240.  
  241. class KUserGroupPrivate;
  242.  
  243. struct group;
  244.  
  245. /**
  246.  * @short Represents a group on your system
  247.  *
  248.  * This class represents a group on your system. You can either get
  249.  * information about the group of the current user, of fetch information about
  250.  * a group on the system. Instances of this class will be explicitly shared,
  251.  * so copying objects is very cheap and you can safely pass objects by value.
  252.  *
  253.  * @author Jan Schaefer <j_schaef@informatik.uni-kl.de>
  254.  * @since 3.3
  255.  */
  256. class KDECORE_EXPORT KUserGroup {
  257.  
  258. public:
  259.  
  260.   /**
  261.    * Create an object from the group of the current user.
  262.    * @param mode if #KUser::UseEffectiveUID is passed the effective user
  263.    *        will be used. If #KUser::UseRealUserID is passed the real user
  264.    *        will be used. 
  265.    *        The real UID will be different than the effective UID in setuid
  266.    *        programs; in  such a case use the effective UID for checking
  267.    *        permissions, and the real UID for displaying information about
  268.    *        the group associated with the user.
  269.    */
  270.   explicit KUserGroup(KUser::UIDMode mode = KUser::UseEffectiveUID);
  271.   
  272.   /**
  273.    * Create an object from a group id.
  274.    * If the group does not exist, isValid() will return false.
  275.    * @param gid the group id
  276.    */
  277.   // XXX KDE4: Give parameter as gid_t instead of "long"
  278.   explicit KUserGroup(long gid);
  279.   
  280.   /**
  281.    * Create an object from a group name.
  282.    * If the group does not exist, isValid() will return false.
  283.    * @param name the name of the group
  284.    */
  285.   explicit KUserGroup(const QString& name);
  286.   
  287.   /**
  288.    * Create an object from a group name.
  289.    * If the group does not exist, isValid() will return false.
  290.    * @param name the name of the group
  291.    */
  292.   explicit KUserGroup(const char *name);
  293.   
  294.   /**
  295.    * Creates an object from a group structure.
  296.    * If the pointer is null, isValid() will return false.
  297.    * @param g the group structure to create the group from.
  298.    */
  299.   explicit KUserGroup(struct group *g);
  300.   
  301.   /**
  302.    * Creates a new KUserGroup instance from another KUserGroup object
  303.    * @param group the KUserGroup to copy
  304.    */
  305.   KUserGroup(const KUserGroup & group);
  306.   
  307.   /**
  308.    * Copies a group
  309.    * @param group the group that should be copied
  310.    * @return this group
  311.    */
  312.   KUserGroup& operator =(const KUserGroup& group);
  313.   
  314.   /**
  315.    * Two KUserGroup objects are equal if isValid() is true
  316.    * and gid() are identical
  317.    * @return true if the groups are identical
  318.    */
  319.   bool operator ==(const KUserGroup& group) const;
  320.   
  321.   /**
  322.    * Two KUserGroup objects are not equal if either 
  323.    * isValid() is not true or gid() are not identical
  324.    * @return true if the groups are not identical
  325.    */
  326.   bool operator !=(const KUserGroup& group) const;
  327.   
  328.   /**
  329.    * Returns wether the group is valid.
  330.    * A KUserGroup object can be invalid if it is 
  331.    * created with a non-existing gid or name.
  332.    * @return true if the group is valid
  333.    */
  334.   bool isValid() const;
  335.   
  336.   /**
  337.    * Returns the group id of the group.
  338.    * @return the group id of the group or -1 if the group is invalid
  339.    */
  340.   // XXX KDE4: Return gid_t instead of "long"
  341.   long gid() const;
  342.   
  343.   /**
  344.    * The name of the group.
  345.    * @return the name of the group
  346.    */
  347.   QString name() const;
  348.   
  349.   /**
  350.    * Returns a list of all users of the group.
  351.    * @return a list of all users of the group
  352.    */
  353.   const QValueList<KUser>& users() const;
  354.   
  355.   /**
  356.    * Returns a list of all user login names of the group.
  357.    * @return a list of all user login names of the group
  358.    */
  359.   QStringList userNames() const;
  360.   
  361.   
  362.   /**
  363.    * Destructor.
  364.    */
  365.   ~KUserGroup(); 
  366.   
  367.   /**
  368.    * Returns a list of all groups on this system
  369.    */
  370.   static QValueList<KUserGroup> allGroups();
  371.   
  372.   /**
  373.    * Returns a list of all group names on this system
  374.    */
  375.   static QStringList allGroupNames();
  376.   
  377. private:
  378.   KSharedPtr<KUserGroupPrivate> d;
  379.   void fillGroup(struct group* g);
  380.   void fillName(const char* name);
  381. };
  382.  
  383.  
  384. #endif
  385.